//
//  OrderListViewController.m
//  jang
//
//  Created by lee han on 29/05/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "OrderListViewController.h"
#import "OrderListCell.h"
#import "CommonView.h"
#import "OrderDetailController.h"

@interface OrderListViewController ()

@end

@implementation OrderListViewController

- (void)requestOrderList:(NSInteger) type {
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 [NSString stringWithFormat:@"%ld", (long)type],  @"ordertype",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_OrderList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    mListData = [[NSMutableArray alloc] init];
    mTextPrompt = @"加载中";
    
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
    
    [_mTableView registerNib:[UINib nibWithNibName:@"OrderListCell" bundle:nil] forCellReuseIdentifier:@"OrderListCell"];
    mType = 1;
    mSegueIdentifier = @"Unpayed2Detail";
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    mTextPrompt = @"加载中";
    [self requestOrderList:mType];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    mSelectedData = [mListData objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:mSegueIdentifier sender:self];
}

//end UITableViewDelegate

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([mListData count] > 0) {
        return [mListData count];
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([mListData count] > 0) {
        OrderListCell *cell = [_mTableView dequeueReusableCellWithIdentifier:@"OrderListCell" forIndexPath:indexPath];
        
        [cell initWithData:[mListData objectAtIndex:indexPath.row]];
        
        return cell;
    }
    
    UITableViewCell* nonecell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    nonecell.backgroundColor = [UIColor clearColor];
    
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    
    button.backgroundColor = [UIColor clearColor];
    
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    [button setTitle:mTextPrompt forState:UIControlStateNormal];
    
    
    [nonecell addSubview:button];
    
    return nonecell;
    
}


//end UITableViewDataSource

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if(type == NetworkFunctionType_OrderList) {
        if([[response objectForKey:@"status"] integerValue] == 1){
            [mListData removeAllObjects];
            if ([response objectForKey:@"data"] != NULL && ![[response objectForKey:@"data"] isEqual:[NSNull null]]) {
                for (NSDictionary* data in [response objectForKey:@"data"]) {
                    [mListData addObject:data];
                }
            }
            mTextPrompt = @"无数据";
            [_mTableView reloadData];
        }
        else{
            [CommonView addAlertView:self Text:@"订单列表错误"];
        }
    }
}
//end networkDelegate


- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender{
    OrderDetailController *destination = segue.destinationViewController;
    
    destination.mOrderInfo = mSelectedData;
    destination.mType = mType;
}
@end
